package com.dao;

import com.domain.Employee;
import com.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class EmployeeDao {
    //创建jdbcTemplate对象
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 添加员工
     * @param employee Employee类型的对象
     * @return Integer类型的数据，1为成功，0为未成功
     */
    public Integer addEmployee(Employee employee) {
        //添加语句
        String sql = "insert into Employee values(?,?,?,?,?,?,?)";
        int update = jdbcTemplate.update(sql,
                employee.getEmployeeId(),
                employee.getEmployeeName(),
                employee.getEmployeePassword(),
                employee.getEmployeePhone(),
                employee.getEmployeeAddress(),
                employee.getEmployeeGrade(),
                employee.getDepartmentId()
        );
        return update;
    }

    /**
     * 根据id删除员工
     * @param id Integer类型的员工id
     * @return Integer类型的数据，1为成功，0为未成功
     */
    public Integer deleteEmployee(Integer id) {
        //删除语句
        String sql = "delete from Employee where employeeid=?";
        //进行执行
        int update = jdbcTemplate.update(sql, id);
        //返回结果
        return update;

    }


    /**
     * 更新员工数据数据
     * @param employee Employee类型的对象
     * @return Integer类型的数据，1为成功，0为未成功
     */
    public Integer updateEmployee(Employee employee) {
        //更新
        String sql = "update employee set employeeName=?,EmployeePassword=?,EmployeePhone=?,EmployeeAddress=? ,EmployeeGrader=? ,DepartmentId=? where EmployeeId=?";
        int update = jdbcTemplate.update(sql,
                employee.getEmployeeName(),
                employee.getEmployeePassword(),
                employee.getEmployeePhone(),
                employee.getEmployeeAddress(),
                employee.getEmployeeGrade(),
                employee.getDepartmentId(),
                employee.getDepartmentId());
        return update;
    }

    /**
     * 查询方法,查询所有员工
     * @return  返回List 数组封装的Employee对象
     */
    public List<Employee> findAllEmployee() {
        //SQl
        String sql = "select * from Employee";
        //new BeanPropertyRowMapper<>(Activity.class)进行查询
        List<Employee> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class));

        return list;
    }

    //按照员工的号码和员工的密码查询员工信息
    /**
     * 按照员工号码查询
     * @param employeePhone String类型的员工号码
     * @param employeePassword String类型的员工密码
     * @return 返回List 数组封装的Employee对象
     */
    public List<Employee> findEmployeeName(String employeePhone,String employeePassword) {
        //sql
        String sql = "select * from employee where employeePassword=? and employeePhone=?";
        //进行查询
        List<Employee> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class),employeePassword,employeePhone);
        return list;
    }

    /**
     * 按照员工号码查询
     * @param phone String类型的员工号码
     * @return 返回Employee类型的对象
     */
    public Employee findEmployee(Integer phone) {
        //sql
        String sql = "select * from Employee where Employeephone=?";
        //进行查询
        List<Employee> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class), phone);

        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 提供员工登录验证方法
     * @param phone String类型的员工号码
     * @param password String类型的员工密码
     * @return 返回Employee类型的对象
     */
    public Employee loginEmployee(String phone, String password) {
        String sql = "select * from employee where employeephone=? and employeepassword=?";
        List<Employee> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class), phone, password);
        //只能有一个数据条
        if (list == null || list.size() == 0) {
            return null;
        } else {
            //返回第一条数据
            return list.get(0);
        }
    }

    /**
     * 员工的名字和号码进行查询
     * @param name String 类型的员工姓名
     * @param phone String类型的员工号码
     * @return 返回Employee类型的对象
     */
    public Employee findEmpNamePass(String name, String phone){
        String sql = "select * from employee where employeename=? and employeephone=?";
        List<Employee> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class), name, phone);

        if(list.size()==0 && list==null){
            return null;
        }
        return list.get(0);
    }

    /**
     * 修改员工自己的密码
     * @param employeePassword String 类型的员工号码
     * @param employeeName String 类型的员工姓名
     * @param employeePhone String 类型的员工电话
     * @return Integer类型的数据，1为成功，0为未成功
     */
    public Integer updateEmpPass(String employeePassword,String employeeName,String employeePhone){
        String sql = "update employee set employeepassword=? where employeename=? and employeephone=?";
        int update = jdbcTemplate.update(sql, employeePassword, employeeName, employeePhone);
        return update;
    }
}
